<?php

namespace App\Admin\Controllers;
/**
 * 分类
 */

use App\Models\Category;
use App\Models\CategoryAdmin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Tree;


class CategoryController extends BaseController
{
    const   INVITE_STATUS = [
        'on'  => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        'off' => ['value' => 1, 'text' => '正常', 'color' => 'default'],
    ];

    public function __construct()
    {
    }

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('分类设置')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('修改分类')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增分类')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $grid = new Grid(new Category());
        $grid->model()->orderBy('created_at', 'desc');
        $grid->disableRowSelector();
//        $grid->number('序号');
//        $grid->id('序号');
        $grid->column('name', '分类名称');
        $grid->column('status', __('状态'))->switch(self::INVITE_STATUS);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->like('name', '名称');
            });

        });
        $grid->expandFilter();
        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉查看
            $actions->disableView();

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Category::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Category());
        $form->display('id', '序号');
//        $form->select('pid', '上级')->options(CategoryAdmin::selectOptions());
        $form->text('name', '分类名称');
//        $form->image('icon', '图标');
//        $form->text('desc', '描述')->help('选填');

        $form->radioButton('status', '状态')->options([0 => '禁用', 1 => '正常'])->default(1);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->footer(function ($footer) {

            // 去掉`重置`按钮
//            $footer->disableReset();

            // 去掉`提交`按钮
//            $footer->disableSubmit();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });
        $form->saving(function (Form $form) {
            $level = 1;
            if (!empty($form->pid)) {
                $parentLevel = Category::where('id', $form->pid)->value('level');
                $level       = $parentLevel + 1;
            }
            $form->model()->level = $level;
        });
        return $form;
    }

    protected function tree()
    {
        return CategoryAdmin::tree(function (Tree $tree) {

            $tree->branch(function ($branch) {

                $src = $branch['icon'];

                $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";

                return "{$branch['id']}  - {$branch['name']} {$logo}";

            });
        });
    }


}
